<template>
	<view class="mine">
		<!-- #ifndef MP-ALIPAY -->
			<view class="bg-gradual-blue mine-header" :style="{'padding-top':CustomBar+'px'}">
				<view class="mine-con" v-if="userInfo">
					<image class="avatar" :src="userInfo.avatar_url"></image>
					<view class="name">
						{{userInfo.loginname}}
					</view>
				</view>
				<button v-else class="cu-btn round bg-gradual-green login-btn" @tap="toLogin">登录</button>
			</view>
		<!-- #endif -->
		<!-- #ifdef MP-ALIPAY -->
			<view class="bg-gradual-blue mine-header">
				<view class="mine-con" v-if="userInfo">
					<image class="avatar" :src="userInfo.avatar_url"></image>
					<view class="name">
						{{userInfo.loginname}}
					</view>
				</view>
				<button v-else class="cu-btn round bg-gradual-green login-btn" @tap="toLogin">登录</button>
			</view>
		<!-- #endif -->
		<view class="cu-list grid" :class="['col-' + gridCol,gridBorder?'':'no-border']">
			<view class="cu-item" v-for="(item,index) in cuIconList" :key="index" v-if="index<gridCol*2" @tap="toPage(index)">
				<view :class="['cuIcon-' + item.cuIcon,'text-' + item.color]">
					<view class="cu-tag badge" v-if="item.badge!=0">
						<block v-if="item.badge!=1">{{item.badge>99?'99+':item.badge}}</block>
					</view>
				</view>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="margin-top padding" v-if="userInfo">
			<button class="bg-gradual-blue xl" @tap="logOut">退出</button>
		</view>
	</view>
</template>

<script>
	import getApp from '../../App.vue'
	export default {
		data() {
			return {
				userInfo: '',
				CustomBar: this.CustomBar,
				cuIconList: [{
					cuIcon: 'notificationfill',
					color: 'olive',
					badge: 0,
					name: '未读消息'
				}, {
					cuIcon: 'recordfill',
					color: 'orange',
					badge: 0,
					name: '个人消息'
				}, {
					cuIcon: 'picfill',
					color: 'yellow',
					badge: 0,
					name: '主题收藏'
				}],
				gridCol: 3,
				gridBorder: false,
			};
		},
		methods: {
			toLogin() { //登录
				uni.navigateTo({
					url: '../login/login'
				});
			},
			async getMessCount() { // 获取未读消息数量
				const res = await this.$api.getMessCount(getApp.globalData.accesstoken)
				this.cuIconList[0].badge = res;
			},
			toPage(i) { //跳转页面
				if (i === 0) {
					uni.navigateTo({
						url: '../messages/messages'
					});
					return;
				} else if (i === 1){
					uni.navigateTo({
						url: '../personMess/personMess?user=' + getApp.globalData.userInfo.loginname
					});
					return;
				} else if (i === 2){
					uni.navigateTo({
						url: '../collections/collections?user=' + getApp.globalData.userInfo.loginname
					});
					return;
				} else {
					uni.showToast({
						title: '期待开放',
						icon: 'none'
					});
				}
			},
			logOut() { //退出登录
				uni.clearStorageSync();
				getApp.globalData = {};
				Object.assign(this,this.$options.data());
				// 将当前状态的data重置为初始状态,this获取当前状态下的data,this.$options.data()获取该组件初始状态下的data
			}
		},
		onShow() {
			this.userInfo = getApp.globalData.userInfo;
			this.getMessCount()
			// #ifdef MP-ALIPAY
				my.setCanPullDown({
					canPullDown: false
				})
			// #endif
		}
	}
</script>

<style lang="scss">
	.mine-header {
		box-sizing: content-box;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.mine-con {
		.avatar {
			width: 120rpx;
			height: 120rpx;
			margin-top: 60rpx;
			border-radius: 50%;
		}
		.name {
			margin: 15rpx 0 60rpx;
			text-align: center;
		}
	}
	.login-btn {
		margin: 60rpx 0;
	}
</style>
